package com.yshebkb.sulp.dao;

import com.yshebkb.sulp.entity.Role;
import com.yshebkb.sulp.entity.UserRole;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author yshebkb
 */
public interface UserRoleMapper {
    @Delete({
            "delete from user_role",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
            "insert into user_role (id, user_id, ",
            "role_id, status, create_time, ",
            "update_time)",
            "values (#{id,jdbcType=BIGINT}, #{userId,jdbcType=BIGINT}, ",
            "#{roleId,jdbcType=BIGINT}, #{status,jdbcType=TINYINT}, #{createTime,jdbcType=TIMESTAMP}, ",
            "#{updateTime,jdbcType=TIMESTAMP})"
    })
    int insert(UserRole record);

    int insertSelective(UserRole record);

    @Select({
            "select",
            "id, user_id, role_id, status, create_time, update_time",
            "from user_role",
            "where id = #{id,jdbcType=BIGINT}"
    })
    UserRole selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(UserRole record);

    @Update({
            "update user_role",
            "set user_id = #{userId,jdbcType=BIGINT},",
            "role_id = #{roleId,jdbcType=BIGINT},",
            "status = #{status,jdbcType=TINYINT},",
            "create_time = #{createTime,jdbcType=TIMESTAMP},",
            "update_time = #{updateTime,jdbcType=TIMESTAMP}",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(UserRole record);

    @Select("select b.* from user_role a left join role b on a.role_id=b.id where a.user_id=#{userId}")
    List<Role> findRolesByUserId(Long userId);
}